package com.sy.springboottest.action;

import java.net.http.HttpRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.IntStream;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

import org.apache.tomcat.util.json.JSONParser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.sy.springboottest.pojo.Demo;

import freemarker.core.JSONOutputFormat;

@Controller
@RequestMapping("/")
public class TestController {
	@GetMapping("j/index")
	public String jIndex(Model model) {
		System.out.println("jsp index");
		model.addAttribute("msg", "hello jsp:" + LocalDateTime.now());
		return "jsp/index";
	}

	@GetMapping("t/index")
	public String tIndex(Model model) {
		System.out.println("t index");
		model.addAttribute("msg", "hello t:" + LocalDateTime.now());
		return "thymeleaf/index";
	}

	@GetMapping("f/index")
	public String fIndex(Model model) {
		System.out.println("jsp index");
		model.addAttribute("msg", "hello jsp:" + LocalDateTime.now());
		return "freemarker/index";
	}

	@GetMapping("j/list")
	public String jList(Model model) {
		model.addAttribute("list", getList("jsp"));
		return "jsp/index";
	}

	@GetMapping("t/list")
	public String tList(Model model) {
		model.addAttribute("list", getList("thymeleaf"));
		return "thymeleaf/index";
	}

	@GetMapping("f/list")
	public String fList(Model model) {
		model.addAttribute("list", getList("freemarker"));
		return "freemarker/index";
	}

	@GetMapping("j/pageList")
	public String jPageList(Model model, HttpServletRequest request) {
		List<Demo> allDs = getList("jsp");
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
		int start = (currentPage - 1) * pageSize;
		int end = currentPage * pageSize;
		if(end>allDs.size()) {
			end = allDs.size();
		}
		List<Demo> subList = allDs.subList(start, end);
		model.addAttribute("pagelist", subList);
		model.addAttribute("list", allDs);
		model.addAttribute("currentPage", currentPage);
		model.addAttribute("pageSize", pageSize);
		return "jsp/index";
	}
	@GetMapping("j/pageListDemo")
	public String jPageListDemo(Model model, HttpServletRequest request) {
		List<Demo> allDs = getList("jsp");
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
		String callBack = request.getParameter("callBack");
		int start = (currentPage - 1) * pageSize;
		int end = currentPage * pageSize;
		if(end>allDs.size()) {
			end = allDs.size();
		}
		List<Demo> subList = allDs.subList(start, end);
		model.addAttribute("pagelist", subList);
		model.addAttribute("list", allDs);
		model.addAttribute("currentPage", currentPage);
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("callBack", callBack);
		return "jsp/dialogDemo";
	}
	

	private List<Demo> getList(String name) {
		return IntStream.range(1, 101).mapToObj(i -> new Demo(i + "", name + "_name_" + i)).toList();
	}
	
	/**-----------------------------------非json的类型--------------------------------------------------**/
	/**
	 * 直接按名称接收参数
	 * http://localhost:8080/demo/getUrlParams?name=zhangsan&age=12
	 * 或者
	 *http://localhost:8080/demo/postBodyParams
	 *body中  name=zhangsan&age=12
	 *get和post都可以
	 * @param name
	 * @param age
	 */
	@RequestMapping("/demo/getParamsByDirect")
	public void getParamsByDirect(String name,Integer age) {
		System.out.println(name +"--->"+age);
	}
	
	/**
	 * 通过@RequestParam注解获取参数
	 * get http://localhost:8080/demo/getParamsByRequestParam?name=12&age=1
	 * 或者
	 * post http://localhost:8080/demo/getParamsByRequestParam
	 * body中 name=zhangsan&age=12
	 */
	@RequestMapping("/demo/getParamsByRequestParam")
	public void getParamsByRequestParam(@RequestParam("name") String name,@RequestParam("age") Integer age) {
		System.out.println(name +"--->"+age);
	}
	
	/**
	 * http://localhost:8080/demo/getParamsByPathVariable/zhangsan/12
	 * @PathVariable 针对url路径中获取参数内容
	 * @param name
	 * @param age
	 */
	@RequestMapping("/demo/getParamsByPathVariable/{name}/{age}")
	public void getParamsByPathVariable(@PathVariable String name,@PathVariable Integer age) {
		System.out.println(name +"--->"+age);
	}
	
	/**
	  * get http://localhost:8080/demo/getParamsByRequest?name=12&age=1
	 * 或者
	 * post http://localhost:8080/demo/getParamsByRequest
	 * body中 name=zhangsan&age=12
	 */
	@RequestMapping("/demo/getParamsByRequest")
	public void getParamsByRequest(HttpServletRequest request) {
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		System.out.println(name +"--->"+age);
	}
	
	/**-----------------------------------json的类型--------------------------------------------------**/
	/**
	 * http://localhost:8080/demo/getParamsByRequstBody
	 * Content-Type: application/json
	 * body {name:'zhangsan',id:12}
	 * @param demo
	 */
	@RequestMapping("/demo/getParamsByRequstBody")
	public void getParamsByRequstBody(@RequestBody Demo demo) {
		System.out.println(JSONObject.toJSON(demo));
	}
}
